package com.alpha.servlets;

import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;

import javax.naming.NamingException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.HttpConstraint;
import javax.servlet.annotation.ServletSecurity;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;

import com.alpha.bom.Exam;
import com.alpha.bom.Professor;
import com.alpha.bom.Student;
import com.alpha.bom.User;
import com.alpha.dao.ExamDao;

/**
 * Servlet implementation class HomeServlet
 */
@WebServlet("/HomeServlet")
@ServletSecurity(@HttpConstraint(rolesAllowed = {"admin","user"}))
public class HomeServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	
	private static Logger logger = Logger.getLogger(HomeServlet.class.getName());
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public HomeServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		logger.info(this.getClass().getSimpleName()+" get method started.");
		
		User user = null; 
		HttpSession session = request.getSession();
		
		String message=null;
		String error = null;
		String success = null;
		
		if(request.getParameter("success")!=null){
			success = request.getParameter("success");
		} else if(request.getParameter("error")!=null){
			error = request.getParameter("error");
		} else if(request.getParameter("message")!=null){
			message = request.getParameter("message");
		}
		
		if(session.getAttribute("user")!=null && (session.getAttribute("user") instanceof Professor)){
			user = (Professor)session.getAttribute("user");
			ExamDao eDao = new ExamDao();
			try {
				((Professor)user).setExams(eDao.retrieveExamByProfessorId(user.getUserId()));
			} catch (NamingException e) {
				logger.error(e.getMessage());
				return;
			} catch (SQLException e) {
				logger.error(e.getMessage());
				return;
			}
			if(error!=null){
				response.sendRedirect("Home.jsp?error="+error);
			} else if(success!=null){
				response.sendRedirect("Home.jsp?success="+success);
			} else if(message!=null){
				response.sendRedirect("Home.jsp?message="+message);
			} else {
				response.sendRedirect("Home.jsp");
			}
		} else if(session.getAttribute("user")!=null && (session.getAttribute("user") instanceof Student)){
			ArrayList<Exam> exams = new ArrayList<Exam>();

			user = (Student)session.getAttribute("user");
			
			ExamDao eDao = new ExamDao();
			
			try {
				exams = eDao.retrieveUntakenExamByStudentId(user.getUserId());
			} catch (NamingException e) {
				logger.error(e.getMessage());
				return;
			} catch (SQLException e) {
				logger.error(e.getMessage());
				return;
			}
			
			if(exams!=null){
				request.setAttribute("exams", exams);
			}
			RequestDispatcher dis=request.getRequestDispatcher("Home.jsp");
            dis.forward(request, response);
			return;
		} else {
			response.sendRedirect("Login.jsp?message=User+not+found.");
			return;
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

}
